﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Handday.Erp.Core
{
    /// <summary>
    /// IModelCacheKeyFactory
    /// </summary>
    public class MyModelCacheKeyFactory<TDbContext> : IModelCacheKeyFactory where TDbContext : DbContext
    {
        public object Create(DbContext context)
        {
            return new MyModelCacheKey<TDbContext>(context);
        }
    }

    /// <summary>
    /// ModelCacheKey
    /// </summary>
    public class MyModelCacheKey<TDbContext> : ModelCacheKey where TDbContext : DbContext
    {
        private readonly string _key;
        /// <summary>
        /// ctor
        /// </summary>
        public MyModelCacheKey(DbContext context)
            : base(context)
        {
            var info = (context as BaseDbContext<TDbContext>).SplitTableInfo;
            _key = info?.Prefix ?? string.Empty + info?.Suffix ?? string.Empty;
        }

        /// <summary>
        /// Equals
        /// </summary>
        protected override bool Equals(ModelCacheKey other)
        {
            return _key == (other as MyModelCacheKey<TDbContext>)?._key;
        }

        /// <summary>
        /// GetHashCode
        /// </summary>
        public override int GetHashCode()
        {
            return _key.GetHashCode();
        }
    }
}
